﻿/* 功能:  申请提现JS
 * 创建人：Kencery  创建时间：2018-2-4
 */
var ml = window.ml || {};
ml.admin = ml.admin || {};

ml.admin.merchantsettlment = (function () {
    var defaults = {
        listUrl: "/Settlement/ListMerchant",
        getReceivablesByMerchantIdUrl: "/Receivables/GetReceivablesByMerchantId",
        applyWithdrawUrl: "/Settlement/ApplyWithdraw",
        setStatusOKUrl: "/Settlement/ApplyWithdraw",
        setStatusFailureUrl: "/Settlement/ApplyWithdraw",
        setStatusCancelUrl: "",
        MerchantId: "",
    };
    var opt = {};

    var columns = [{
        field: 'state',
        radio: true
    }, {
        title: '行号',
        field: 'number',
        align: 'center',
        hide: true,
        formatter: plscommon.tableNumber
    }, {
        title: '结算编号',
        field: 'Number'
    }, {
        title: '总金额',
        field: 'OrderAmount',
        align: 'center',
    }, {
        title: '待结算金额',
        field: 'CanSettlementAmount',
        align: 'center',
    }, {
        title: '申请结算金额',
        field: 'ActSettlementAmount',
        align: 'center',
    }, {
        title: '申请结算人',
        field: 'Creater',
        align: 'center',
    }, {
        title: '创建时间',
        field: 'CreateTime',
        align: 'center',
    }, {
        title: '结算状态',
        field: 'StatusName',
        align: 'center',
    }, {
        title: '结算操作人',
        field: 'Optioner',
        align: 'center',
    }, {
        title: '结算时间',
        field: 'SettlementTime',
        align: 'center',
    }];

    var initTable = function () {
        plscommon.bootstraptable({
            id: "#settlementinfo",
            url: defaults.listUrl,
            queryParams: queryParams,
            uniqueId: "Id",
            columns: columns
        });
    };

    var queryParams = function (params) {
        return {
            offset: params.offset,                  //后台计算显示数据信息
            pagesize: params.limit,                 //每页显示多少行
            number_search: $("#number_search").val(),
            settlementstatus_search: $("#settlementstatus_search").val()
        };
    }

    var initDropDownList = function () {
        plscommon.dropDownList("#ReceivablesId", defaults.getReceivablesByMerchantIdUrl);
    }

    var clickEvent = function () {
        $("#btnQueryList").on("click", function () { btnQueryList(); });                            //按条件查询结果
        $("#btnReset").on("click", function () { btnReset(); });                                    //清空文本框信息

        $("#btnApplyWithdrawDialog").on("click", function () { btnApplyWithdrawDialog() });         //申请体现弹出浮层
        $("#btnApplyWithdrawOK").on("click", function () { btnApplyWithdrawOK() });                 //申请体现

        $("#btnFailureDialog").on("click", function () { btnFailureDialog(); });                    //失败取消弹层
    }

    var btnQueryList = function () {
        plscommon.refreshTable("settlementinfo");
    }

    var btnReset = function () {
        plscommon.resetFrom("formSearch");
    }

    var btnApplyWithdrawDialog = function () {
        if (defaults.MerchantId == "") {
            plscommon.warningMessage("该商户还未配置店铺信息，不允许申请体现", 4000);
            return false;
        }
        $('#ApplyWithdrawModal').modal();
        plscommon.resetFrom("ApplyWithdrawOperation");
    }

    var btnApplyWithdrawOK = function () {
        var data = plscommon.deleteByValue($("#ApplyWithdrawOperation").serializeArray(), "Id")
        data = plscommon.transitionArray(data);
        if (!checkData(data)) {
            return false;
        }

        $("#btnApplyWithdrawOK").prop("disabled", true);
        plscommon.ajax_status({
            disableId: "btnApplyWithdrawOK",
            url: defaults.applyWithdrawUrl,
            type: "POST",
            data: data,
            success: function () {
                if (this.status_code === 200) {
                    $('#ApplyWithdrawModal').modal('hide');
                    plscommon.refreshTable("settlementinfo");
                } else {
                    plscommon.warningMessage(this.status_message, 3000);
                }
            }
        });
    }

    var btnFailureDialog = function () {
        //结算单为已提交能操作，否则不能操作
        var data = plscommon.dialogWaringDisable('settlementinfo', 'FailureOperation');
        if (!data) {
            return false;
        }
        if (data.SettlementStatus == 1 || data.SettlementStatus == 2) {
            plscommon.warningMessage("该结算单已提交或者已完成，没有失败原因", 3000);
            return false;
        }
        $("#FailureModal").modal();
        $("#Remark").text(data.Remark);
    }

    var checkData = function (data) {
        if (!data.ReceivablesId) {
            plscommon.warningMessage("请您选择收款方式，不允许申请体现", 3000);
            return false;
        }
        if (!data.CanSettlementAmount || parseFloat(data.CanSettlementAmount) <= 0) {
            plscommon.warningMessage("您没有能够结算的金额，不允许申请体现", 3000);
            return false;
        }
        if (!data.ActSettlementAmount) {
            plscommon.warningMessage("申请体现额不能为空，请您检查", 3000);
            return false;
        }
        if (!plscommon.extend.price().test(data.ActSettlementAmount)) {
            plscommon.warningMessage("请您输入标准的金钱申请额，请您检查", 3000);
            return false;
        }
        if (data.ActSettlementAmount <= 0) {
            plscommon.warningMessage("您申请的额度必须>0，请您检查", 3000);
            return false;
        }
        if (parseFloat(data.ActSettlementAmount) > parseFloat(data.CanSettlementAmount)) {
            plscommon.warningMessage("您没有那么多金额需要体现，请您检查，请您检查", 3000);
            return false;
        }
        return true;
    }

    return {
        init: function (options) {
            $.extend(defaults, options || {});
            initTable();            //初始化Table表格
            clickEvent();           //触发事件
            initDropDownList();     //初始化下拉事件
        }
    };
}());